package com.diyizu.controller.impl;

import com.diyizu.controller.ILoginController;
import com.diyizu.controller.IStudentInfoController;
import com.diyizu.dao.impl.ReadLogDao;
import com.diyizu.factory.Factory;
import com.diyizu.service.ILoginService;
import com.diyizu.service.impl.ILoginServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class ILoginControllerImpl implements ILoginController {
    private static Scanner sc = new Scanner(System.in);
    private static final Logger LOGGER = LoggerFactory.getLogger(ILoginControllerImpl.class);
    private static ILoginService iLoginService = Factory.getILoginService();
    IStudentInfoController studentInfoController = Factory.getStudentInfoController();
    ReadLogDao readLogDao = Factory.getReadLogDao();

    @Override
    //登录
    public void userLogin() {
        while (true) {
            System.out.println("请输入用户名");
            String userName = sc.next();
            if (iLoginService.isLoginNum(userName)){
                if (judgePass(userName)) {
                    return;
                }
            }else {
                //输错三次
                ArrayList<String> log = readLogDao.getLog();
                for (int i = log.size() - 1; i >= 0; i--) {
                    if (log.get(i).endsWith(userName+"用户登录失败")){
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            //获取最后一次登录失败的时间
                            Date endLoginTime = sdf.parse(log.get(i).split(",")[0]);
                            long haoshi;
                            if ((haoshi = System.currentTimeMillis()-endLoginTime.getTime())<=1200000L){
                                System.out.println("您输入错误达到三次,请"+((1200000-haoshi)/60000)+"分钟后重试");
                                return;
                            }else {
                                break;
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
                //如果时间超过20分钟，则让他重新登录一次
                if (judgePass(userName)) {
                    return;
                }

            }


        }
    }

    @Override
    public String getUserName() {
        ArrayList<String> log = readLogDao.getLog();
        String s = "";
        for (int i = log.size() - 1; i >= 0; i--) {
            if (log.get(i).endsWith("用户登录成功")){
                s = log.get(i);
                break;
            }
        }
        String[] split = s.split(" ");
        String userName = split[6].split("用户")[0];
        return userName;
    }

    private boolean judgePass(String userName) {
        System.out.println("请输入密码");
        String passWord = sc.next();
        boolean b = iLoginService.ifPassWord(userName, passWord);
        if (b){
            String loginTime = null;
            ArrayList<String> log = readLogDao.getLog();
            for (int i = log.size() - 1; i >= 0; i--) {
                if (log.get(i).endsWith(userName + "用户登录成功")){
                    loginTime = log.get(i).split(",")[0];
                    break;
                }
            }
            if (loginTime==null){
                System.out.println("欢迎"+userName+"登录"+"这是您第一次登录");
            }else {
                System.out.println("欢迎"+userName+"登录"+",您上次登录时间为:"+loginTime);
            }
            LOGGER.info(userName+"用户登录成功");
            studentInfoController.start();
            return true;
        }else {
            LOGGER.info(userName+"用户登录失败");
            System.out.println("你输入的用户名或密码不正确，请重新输入");
        }
        return false;
    }

}
